<?php

/*
 * (1). 显示字段：城市id ，城市名， 所属省份
(2). 统计每个省份有多少个城市，显示字段：省份id ，省份名，包含多少个城市。
(3). 找出城市数量小于15座的省份
 */
select a.id,a.city,b.province from city as a join  province as b on a.province_id = b.id

die;
/*
 * （编程题）要求写一段程序，实现以下数组$arr1转换成数组$arr2
 */

function conversionArr($data = array(),$fid=0){
    $list = [];
    $keys = array_column($data,'fid');
    foreach ($keys as $k=>$v){
        foreach ($data as $key=>$val){
            if((int)$v===(int)$val['fid']){
                $temp = $val;
                unset($temp['fid']);
                $list[] = array($val);
            }
        }
    }
return $list;
}



/*
 * （编程题）写一个函数，输入日期，格式如2019-08-23，求当前是这一年的第几天（以1月1号为第一天）。
 */
function getDay($date='') {
    $time = strtotime($date);
    $year = date('Y',$time);
    $month = date('m',$time);
    $day = date('d',$time);
    $sum = -1;
    switch ($month) {
        case 1:
            $sum = 0;
            break;
        case 2:
            $sum = 31;
            break;
        case 3:
            $sum = 59;
            break;
        case 4:
            $sum = 90;
            break;
        case 5:
            $sum = 120;
            break;
        case 6:
            $sum = 151;
            break;
        case 7:
            $sum = 181;
            break;
        case 8:
            $sum = 212;
            break;
        case 9:
            $sum = 243;
            break;
        case 10:
            $sum = 273;
            break;
        case 11:
            $sum = 304;
            break;
        case 12:
            $sum = 334;
            break;
        default:
            echo '输入错误，请输入1-12之间的数';
            break;
    }
    if ($sum >= 0) {
        $sum += $day;
        if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 0)) {
          $leap = 1;
} else {
            $leap = 0;
        }
        if ($leap == 1 && $month == 2) {
            $sum++;
        }
        return $sum;
    }

}

$result = getDay('2019-08-23');
print_r($result);
die;
/*
 *1 （编程题）写一个函数，将一个整数转换为千分位形式，每三位用空格符隔开。
例如把字符串1234567890转换为1 234 567 890
 */
function conversionStr($str) {
    $newStr = strrev($str);
    $newStr = chunk_split($newStr, 3, ' ');
    return strrev(rtrim($newStr, ','));
}

$result = conversionStr('1234567890');
print_r($result);
